package com.budgetme.ui.fragments;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import com.budgetme.R;
import com.budgetme.activities.elements.BudgetProgressBar;
import com.budgetme.activities.elements.BudgetProgressBar2;
import com.budgetme.database.Database;
import com.budgetme.database.Provider;
import com.budgetme.graphing.BudgetExpenseGraph;
import com.budgetme.graphing.GraphViewSeries;
import com.budgetme.graphing.LineGraphView;
import com.budgetme.graphing.GraphViewSeries.GraphViewData;

import android.app.Activity;
import android.app.Fragment;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;


// TODO: Add a menu option to view expenses, buckets, etc

public class BudgetDetailsFragment extends DetailsFragment {
	private static final String TAG = "BudgetDetailsFragment";
	
	private OnEditBudgetSelectedListener mEditBudgetSelectedListener;
		
	private TextView mBudgetNameField;
	private TextView mBudgetAmountField;
	private TextView mBudgetCycleField;
	
	private LinearLayout mGraphView;
	
	private double mBudget;
	
	private int mDaysInMonth;
	private int mDayOfMonth;
	
	private int mLifecycle;
		
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
    	Log.d(TAG, "onCreateView");
    	
    	// Inflate the layout for this fragment
    	View fragment = inflater.inflate(R.layout.budget_details_fragment, container, false);
    	
    	mBudgetNameField = (TextView) fragment.findViewById(R.id.budget_title);
		mBudgetAmountField = (TextView) fragment.findViewById(R.id.budget_amount);
		mBudgetCycleField = (TextView) fragment.findViewById(R.id.budget_cycle);

		mGraphView = (LinearLayout) fragment.findViewById(R.id.graph_layout);
								
		return fragment;
    
    }
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		if (!mHasData) {
			// TODO: Need to force text this
			Log.w(TAG, "No bucket was found in the database with ID: " + mId);
			return;
		}
		
		mLifecycle = mDataCursor.getInt(mDataCursor.getColumnIndex(Database.tbl_budgets.t_lifecycle));
		int cycleDays = Provider.getLifecyleDays(mLifecycle);
		mBudgetCycleField.setText(String.valueOf(cycleDays) + " Days");
		
		
		
		
		
		Uri uri = Uri.withAppendedPath(Database.tbl_expenses.CONTENT_URI, "sum/" + String.valueOf(mId));
		Cursor cursor = mContentResolver.query(uri, null, null, null, null);
		
		mBudget = mDataCursor.getDouble(mDataCursor.getColumnIndex(Database.tbl_budgets.t_budget));
		
		double spent = 0.0;
		if (cursor.moveToFirst()) {
			spent = cursor.getDouble(cursor.getColumnIndex(Database.tbl_expenses.e_amount));
		}
		
		Log.e(TAG, "SPENT: " + String.valueOf(spent));
		
		boolean enabled = (mDataCursor.getInt(mDataCursor.getColumnIndex(Database.tbl_budgets.t_enabled)) == 1) ? true : false;
		mLifecycle = mDataCursor.getInt(mDataCursor.getColumnIndex(Database.tbl_budgets.t_lifecycle));
		
        mDataCursor.close();
        
        Calendar calendar = Calendar.getInstance();
        mDaysInMonth = calendar.getMaximum(Calendar.DAY_OF_MONTH);
        mDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

        double remaining = mBudget - spent;
        double progress = ((mBudget / mDaysInMonth) * mDayOfMonth) - spent;
        
//		mBucketRemainingField.setText(String.format("%.0f", remaining));
//		mBucketProgressField.setText(String.format("%.0f", progress));
//		mBucketSpentField.setText(String.format("%.0f", spent));
		
		loadGraph();
	}
	
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.d(TAG, "onAttach");
//        try {
//        	mEditBudgetSelectedListener = (OnEditBudgetSelectedListener) activity;
//        } catch (ClassCastException e) {
//            throw new ClassCastException(activity.toString() + " must implement OnEditBudgetSelectedListener");
//        }
        
    }
    
    @Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    	menu.clear();
	    inflater.inflate(R.menu.budget_details_fragment_menu, menu);
	    super.onCreateOptionsMenu(menu, inflater);
	}
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.budget_details_menu_edit:
            	mEditBudgetSelectedListener.onEditBudgetSelected();
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    

	
    
    // TODO: Need to fix the graph not updating after submitting an expense
	private void loadGraph() {
		
		BudgetExpenseGraph graphView = new BudgetExpenseGraph(getActivity(), "Title", mId);
		
      	mGraphView.removeAllViews();
      	mGraphView.addView(graphView);
      	
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
	}
	
	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "onPause");
	}
		
	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume");
		loadGraph();
	}
	
	public interface OnEditBudgetSelectedListener {
		public void onEditBudgetSelected();
	}

	@Override
	protected TextView[] getTo() {
		return new TextView[] {mBudgetNameField, mBudgetAmountField};
	}

	@Override
	protected String[] getFrom() {
		return new String[] {Database.tbl_budgets.t_name, Database.tbl_budgets.t_budget};
	}

	@Override
	protected Uri getUri() {
		return Uri.withAppendedPath(Database.tbl_budgets.CONTENT_URI, String.valueOf(mId));
	}

}
